#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

int gcnt = 100;
void *ThreadRoutine(void *arg)
{
    const char *threadname = (const char*)arg;
    while (true)
    {
        std::cout << "i'm a new thread" << threadname << " " << &gcnt << ' '<< gcnt << ' ' << getpid() <<  std::endl;
        gcnt--;
        sleep(1);
    }
    
}
int main()
{
    pthread_t tid4;
    pthread_create(&tid4, nullptr, ThreadRoutine, (void*)"NO.4");
    sleep(3);
    
    pthread_t tid1;
    pthread_create(&tid1, nullptr, ThreadRoutine, (void*)"NO.1");
    sleep(3);

    pthread_t tid2;
    pthread_create(&tid2, nullptr, ThreadRoutine, (void*)"NO.1");
    sleep(3);

    pthread_t tid3;
    pthread_create(&tid3, nullptr, ThreadRoutine, (void*)"NO.1");
    sleep(3);

    while(true)
    {
        std::cout << "I'm main thread" << ", pid:" << getpid() << ' ' << std::endl;
    }
    return 0;
}